#include "e.h"
#include "e_mod_main.h"
#include "itask_menu.h"


static void _itask_menu_cb_mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info);
static void _itask_menu_cb_configuration(void *data, E_Menu *m, E_Menu_Item *mi);
static void _itask_menu_cb_post(void *data, E_Menu *m);
static void _itask_menu_cb_config_post(void *data, E_Menu *m);

static void _itask_menu_item_cb(void *data, E_Menu *m, E_Menu_Item *mi);
static void _itask_menu_icon_cb(void *data, E_Menu *m, E_Menu_Item *mi);

EAPI void
itask_menu_button(Itask *it)
{
      Evas_Coord w, h;
      it->o_button = edje_object_add(evas_object_evas_get(it->o_box));
      evas_object_event_callback_add(it->o_button, EVAS_CALLBACK_MOUSE_DOWN, _itask_menu_cb_mouse_down, it);

      if (!e_theme_edje_object_set(it->o_button, "base/theme/modules/itask", "modules/itask/item"))
         edje_object_file_set(it->o_button, itask_theme_path, "modules/itask/item");

      edje_object_part_text_set(it->o_button, "label", "");
      evas_object_show(it->o_button);
      e_box_pack_start(it->o_box, it->o_button);

      //evas_object_geometry_get(it->o_box, NULL, NULL, &w, &h);

    /*  evas_object_geometry_get(it->inst->gcc->gadcon->o_container,
                NULL, NULL, &w, &h);
     if (e_box_orientation_get(it->o_box))
	       w = h;
	     else
	       h = w;
	       printf("itask_menu_button %d/%d\n",w,h);
	       
	      
	       
      e_box_pack_options_set(it->o_button,
      //                       0, 0, /* fill */
      //                       0, 0, /* expand */
      //                       0.5, 0.5, /* align */
      //                      w, h, /* min */
      //                       9999, 9999 /* max */
      //                      );
}


/* taken from e_int_menus */
static void
_itask_menu_icon_cb(void *data, E_Menu *m, E_Menu_Item *mi)
{
   E_Border *bd;
   Evas_Object *o;

   bd = data;
   E_OBJECT_CHECK(bd);

   o = e_icon_add(m->evas);
   e_icon_object_set(o, e_border_icon_add(bd, m->evas));

   mi->icon_object = o;
}


/* taken from e_int_menus */
static void
_itask_menu_item_cb(void *data, E_Menu *m, E_Menu_Item *mi)
{
   E_Border *bd;

   bd = data;
   E_OBJECT_CHECK(bd);
   if (bd->iconic)
   {
      if (!bd->lock_user_iconify)
         e_border_uniconify(bd);
   }
   e_desk_show(bd->desk);
   if (!bd->lock_user_stacking)
      e_border_raise(bd);
   /*if (!bd->lock_focus_out)
   {
      e_border_focus_set(bd, 1, 1);
   }*/
}



static void
_itask_menu_cb_mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
   Evas_Event_Mouse_Down *ev;
   Itask *it;
   E_Menu *mn = NULL;
   E_Menu_Item *mi;

   int cx, cy, cw, ch;
   Evas_Coord x, y, w, h;
   Instance *inst;

   int dir;

   it = data;
   inst = it->inst;
   ev = event_info;

   if (ev->button == 3)
   {
      mn = e_menu_new();
      e_menu_post_deactivate_callback_set(mn, _itask_menu_cb_config_post, NULL);

      mi = e_menu_item_new(mn);
      e_menu_item_label_set(mi, _("Configuration"));
      e_util_menu_item_edje_icon_set(mi, "enlightenment/configuration");
      e_menu_item_callback_set(mi, _itask_menu_cb_configuration, it);

      e_gadcon_client_util_menu_items_append(it->inst->gcc, mn, 0);
   }
   else if (ev->button == 1)
   {
      Evas_List *l;
      Itask_Item *ic;
      if(evas_list_count(it->items_menu) == 0)
        return;
        
      mn = e_menu_new();
      const char *title;

      for(l = it->items_menu; l ; l = l->next)
      {
         ic = l->data;
         mi = e_menu_item_new(mn);
         title = e_border_name_get(ic->border);

         if ((title) && (title[0]))
            e_menu_item_label_set(mi, title);
         else
            e_menu_item_label_set(mi, _("No name!!"));

         e_menu_item_label_set(mi, title);
         e_menu_item_callback_set(mi, _itask_menu_item_cb, ic->border);
         e_menu_item_realize_callback_set(mi, _itask_menu_icon_cb, ic->border);

      }
      e_menu_post_deactivate_callback_set(mn, _itask_menu_cb_post, NULL);
   }
   if(mn)
   {
      evas_object_geometry_get(it->o_button, &x, &y, &w, &h);
      e_gadcon_canvas_zone_geometry_get(inst->gcc->gadcon,
                                        &cx, &cy, &cw, &ch);
      x += cx;
      y += cy;

      dir = E_MENU_POP_DIRECTION_AUTO;
      switch (inst->gcc->gadcon->orient)
      {
      case E_GADCON_ORIENT_TOP:
      case E_GADCON_ORIENT_CORNER_TL:
      case E_GADCON_ORIENT_CORNER_TR:
         dir = E_MENU_POP_DIRECTION_DOWN;
         break;
      case E_GADCON_ORIENT_BOTTOM:
      case E_GADCON_ORIENT_CORNER_BL:
      case E_GADCON_ORIENT_CORNER_BR:
         dir = E_MENU_POP_DIRECTION_UP;
         break;
      case E_GADCON_ORIENT_LEFT:
      case E_GADCON_ORIENT_CORNER_LT:
      case E_GADCON_ORIENT_CORNER_LB:
         dir = E_MENU_POP_DIRECTION_RIGHT;
         break;
      case E_GADCON_ORIENT_RIGHT:
      case E_GADCON_ORIENT_CORNER_RT:
      case E_GADCON_ORIENT_CORNER_RB:
         dir = E_MENU_POP_DIRECTION_LEFT;
         break;
      case E_GADCON_ORIENT_FLOAT:
      case E_GADCON_ORIENT_HORIZ:
      case E_GADCON_ORIENT_VERT:
      default:
         dir = E_MENU_POP_DIRECTION_AUTO;
         break;
      }
      e_menu_activate_mouse(mn,
                            e_util_zone_current_get(e_manager_current_get()),
                            x, y, w, h,
                            dir, ev->timestamp);

      e_util_evas_fake_mouse_up_later(inst->gcc->gadcon->evas, ev->button);
   }
}



static void
_itask_menu_cb_post(void *data, E_Menu *m)
{
   e_object_del(E_OBJECT(m));
}



static void
_itask_menu_cb_config_post(void *data, E_Menu *m)
{
   e_object_del(E_OBJECT(m));
}



static void
_itask_menu_cb_configuration(void *data, E_Menu *m, E_Menu_Item *mi)
{
   Itask *it;
   Config_Item *ci;
   int ok = 1;
   Evas_List *l;

   it = data;
   ci = itask_config_item_get(it->inst->gcc->id);
   for (l = itask_config->config_dialog; l; l = l->next)
   {
      E_Config_Dialog *cfd;

      cfd = l->data;
      if (cfd->data == ci)
      {
         ok = 0;
         break;
      }
   }
   if (ok)
      _config_itask_module(ci);
}
